package com.hqyj.system.service.impl;

import com.hqyj.system.mapper.SysRoleMapper;
import com.hqyj.system.mapper.SysUserRoleMapper;
import com.hqyj.system.model.SysRole;
import com.hqyj.system.model.SysUserRole;
import com.hqyj.system.service.SysUserRoleService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Auther: ZLF
 * @Date: 2021/12/30 10:58
 * @Description:
 */
public class SysUserRoleServiceImpl implements SysUserRoleService {
    private static SysUserRoleMapper sysUserRoleMapper;
    private static SqlSession sqlSession;
    @Override
    public int addSysUserRoleBySysUserRole(SysUserRole sysUserRole) throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建SqlSessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);
        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysUserRoleMapper = sqlSession.getMapper(SysUserRoleMapper.class);
        //############################ 业务 ################################
        int n = sysUserRoleMapper.insertSelective(sysUserRole);
        //############################ 业务 ################################
        //提交事务
        sqlSession.commit();
        return n;
    }

    @Override
    public int deleteSysUserRoleByUserId(int userId) throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建SqlSessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);
        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysUserRoleMapper = sqlSession.getMapper(SysUserRoleMapper.class);
        //############################ 业务 ################################
        int n = sysUserRoleMapper.deleteSysUserRoleByUserId(userId);
        //############################ 业务 ################################
        //提交事务
        sqlSession.commit();
        return n;
    }
}